package service;
import entity.BookType;
import function.BookTypeBaseDao2Impl;
import function.Druid;
import interfaces.BookTypeServiceDao;
import tool.MyTool;
import tool.TSUtility;
import view.LogView;

import java.util.List;
import java.util.Scanner;

/**
 * 添加图书类型
 */
public class BookTypeServiceDaoImpl implements BookTypeServiceDao{
    Scanner sc=new Scanner(System.in);
    BookTypeBaseDao2Impl bookTypeBaseDao = BookTypeBaseDao2Impl.getBookTypeBaseDao();

    @Override
    public void insert() throws Exception {
        System.out.println("请输入要添加的类型名（直接按确定退出）：");
        String name=sc.nextLine();
        if(name.equals("")){
            System.out.println("退出成功");
            return;
        }
        System.out.println("是否添加（Y/N）？");
        char c = TSUtility.readConfirmSelection();
        if(c=='N'){
            return;
        }
        boolean insert = bookTypeBaseDao.insert(Druid.getConnection(), name);
        if(insert){
            Journal.record(LogView.getOperator().getName(),"添加了读者类型",name);
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }

    /**
     * 删除书本类型
     */
    @Override
    public void deleteById() throws Exception {
        List<BookType> all = getAll();
        System.out.println("请输入要删除书本类型的序号（直接按确定退出）：");
        Integer id = MyTool.getTypeIndex(all.size());
        if(id==null){
            System.out.println("退出成功");
            return;
        }
        System.out.println("是否删除（Y/N）？");
        char c = TSUtility.readConfirmSelection();
        if(c=='N'){
           return;
        }
        boolean b = bookTypeBaseDao.deleteById(Druid.getConnection(),all.get(id).getId());
        if(b){
            BookType bookType = bookTypeBaseDao.getBookTypeById(Druid.getConnection(), id);
            Journal.record(LogView.getOperator().getName(),"删除了读者类型",bookType.getTypeName());
            System.out.println("删除成功");
        }
        else {
            System.out.println("删除失败");
        }
    }


    /**
     * 修改书本类型
     * @throws Exception
     */
    @Override
    public void update(int id) throws Exception {
            BookType type = bookTypeBaseDao.getBookTypeById(Druid.getConnection(), id);
            if(type==null){
                System.out.println("没有该对象");
                return;
            }
            System.out.print("名字（"+type.getTypeName()+"):");
            String name = sc.nextLine();
            if(name.equals("")){
                name=type.getTypeName();
            }else {
                System.out.println("是否要修改（Y/N）？");
            char c = TSUtility.readConfirmSelection();
            if(c=='N'){
                return;
            }
            }
            boolean update = bookTypeBaseDao.update(Druid.getConnection(), id, name);
            if(update){
                Journal.record(LogView.getOperator().getName(),"修改了读者类型",name);
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败，输入了的读者类型编号错误");
            }
        }

    /**
     * 通过编号查询书本类型
     * @return
     */
    @Override
    public Integer getBookTypeById() throws Exception {
        System.out.println("请输入要查找读书类型的id(直接按确定退出)：");
        Integer id = MyTool.readInt();
        if(id==null)
        {
            return null;
        }
        BookType bookTypeById = bookTypeBaseDao.getBookTypeById(Druid.getConnection(), id);
        if(bookTypeById==null){
            System.out.println("没有相关结果");
            return -1;
        }
        Journal.record(LogView.getOperator().getName(),"查询了读者类型",bookTypeById.getTypeName());
        MyTool.printOne(bookTypeById);
        return id;
    }

    /**
     * 通过姓名查询类型
     * @return
     */
    @Override
    public Integer getBookTypeByName() throws Exception {
        System.out.println("请输入要查找读书类型的name(直接按确定退出)：");
        String name =TSUtility.readString(5,"");
        if(name.equals("")){
            return null;
        }
        BookType bookTypeBy = bookTypeBaseDao.getBookTypeByName(Druid.getConnection(),name);
        if(bookTypeBy==null){
            System.out.println("没有相关结果");
            return -1;
        }
        Journal.record(LogView.getOperator().getName(),"查询了读者类型",name);
        MyTool.printOne(bookTypeBy);
    return bookTypeBy.getId();
    }

    /**
     * 查询所有的书本类型
     * @throws Exception
     * @return
     */
    @Override
    public List<BookType> getAll() throws Exception {
        List<BookType> arr = bookTypeBaseDao.getAll(Druid.getConnection());
        if(arr==null){

            System.out.println("还没有书本类型，请先添加");
        }
        Journal.record(LogView.getOperator().getName(),"查询了所有读者类型");
        if(arr!=null)
        MyTool.printArr(arr);
        return arr;
    }







    public static void main(String[] args) {
         BookTypeServiceDaoImpl serviceDao=new BookTypeServiceDaoImpl();
        try {

        } catch (Exception e) {
          e.printStackTrace();
        }
    }
}
